package com.example.demo.webservice.repository;

import com.example.demo.data.employee.Employee;
import com.example.demo.data.employee.EmployeeDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class EmployeeRepository {

    private JdbcTemplate jdbcTemplate;
    @Autowired
    public EmployeeRepository(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
    public List<Employee> getAllEmployeeInfo(){
        String sql = "select * from employee order by empId";
        List<Employee> employeesList = jdbcTemplate.query(sql, new EmployeeDO());
        return employeesList;
    }
    public Employee getEmployeeInfoByEmpID(long empID){
        String sql = "select * from employee where empId = ?";
        return jdbcTemplate.queryForObject(sql, new EmployeeDO(), empID);
    }
    public Employee addEmployeeInfo(Employee employee){
        String sql = "insert into employee(empId, empName, deptId, deptName,auth) values (?,?,?,?,?)";
        jdbcTemplate.update(sql, employee.getEmpID(),employee.getEmpName(),
                employee.getDeptID(),employee.getDeptName(),employee.getAuth());
        return getEmployeeInfoByEmpID(employee.getEmpID());
    }
    public Employee editEmployeeInfo(Employee employee){
        String sql = "update employee set empName=?, deptId=?,deptName=?,auth=? where empId=?";
        jdbcTemplate.update(sql,employee.getEmpName(),employee.getDeptID(),employee.getDeptName(),
                employee.getAuth(),employee.getEmpID());
        System.out.println(employee.getEmpID());
        return getEmployeeInfoByEmpID(employee.getEmpID());
    }
    public ResponseEntity deleteEmployeeInfo(long empID){
        String sql = "delete from employee where empId = ?";
        jdbcTemplate.update(sql, empID);
        return new ResponseEntity<String>("success", HttpStatus.OK);
    }
    public Map<String, Object> QueryByEmpID(long empID){
        String sql = "select * from employee where empId = ?";
        Map<String,Object> employeeMap = jdbcTemplate.queryForMap(sql, empID);
        return employeeMap;
    }
}
